
'This example shows how to create a simple game with game2d.

SuperStrict

Framework wdw.game2d


?Debug
Import lugi.generator
?

'render layers friendly names
Const LAYER0:Int = 0
Const LAYER1:Int = 1

Const GENERATE_GLUE:Int = False
'Const GENERATE_GLUE:Int = True

?Debug
If GENERATE_GLUE = True
	GenerateGlueCode("glue.bmx")
	End
EndIf
?

Include "glue.bmx"


Global engine:TEngine = TEngine.Create(New TMyGame)
engine.SetDisplayMode(800, 600)
engine.Start()


Type TMyGame Extends TGame


	Method New()
		SetTitle("Game2d Artemax Test")
	End Method
	
	
	Method Startup()
	
		AutoMidHandle(True)
		SetMaskColor(0, 0, 0)
		AutoImageFlags(MASKEDIMAGE)
		
		TRenderState.Reset()
'		SetGameResolution(200,150)

		'set up lua environment
		GetScriptSystem().RunScriptFile("start.lua")

		'adjust camera damping	
		GetCameraSystem().SetDamping(0.06)
		'adjust quadtree node entities max amount
		GetQuadTreeSystem().SetMaxNodeEntities(5)		

		'load images
		TResources.StoreImage(LoadImage("../media/ship.png"), "playership", "game")
	
		'add the game state
		Local s:TPlayState = New TPlayState
		AddGameState(s, 0)
		
		'force this call to setup
		s.Enter()
	End Method

End Type


Type TPlayState Extends TGameState
	
	Field _player:TEntity
	Field _ent:TEntity
	Field _collisionSystem:TCollisionSystem
	Field _quadTreeSystem:TQuadTreeSystem

	
	Method Enter()
		
		'get references to system. see PreRender()
		_quadTreeSystem = _game.GetQuadTreeSystem()
		_collisionSystem = _game.GetCollisionSystem()
		
		'create player ship entity in render layer 1
		Local e:TEntity
		e = _world.CreateEntity()
		e.AddComponent(TScriptComponent.Create("script.lua"))
		e.AddComponent(New TCameraComponent)
		e.AddComponent(TColorComponent.Create(255, 0, 0, 1.0))
		e.AddComponent(TTransformComponent.Create(1.0, 1.0))
		e.AddComponent(TPositionComponent.Create(0, 0))
		e.AddComponent(TPhysicsComponent.Create(0.05))
		e.AddComponent(THitBoxComponent.Create(32, 32))
		'label is also image name in resource manager
		' 'game' is the image group
		e.AddComponent(TRenderFormComponent.Create("playership", "game", LAYER1))
		e.AddComponent(New TQuadTreeComponent)
		e.Refresh()
		_player = e
		
		'create some block entities	in render layer 0
		For Local i:Int = 1 To 10
			e = _world.CreateEntity()
			
			Local scale:Float = Rnd(1.0, 3.0)
			e.AddComponent(TTransformComponent.Create(scale, scale, Rand(360)))
			e.AddComponent(TColorComponent.Create(255, 255, 0, 1.0))
			e.AddComponent(TPositionComponent.Create(Rnd(-200, 200), Rnd(-200, 200)))
			e.AddComponent(TRenderFormComponent.Create("playership", "game", LAYER0))
			e.AddComponent(THitBoxComponent.Create(32, 32))
			e.AddComponent(New TQuadTreeComponent)
			e.SetGroup("blocks")
			e.AddComponent(TScriptComponent.Create("block.lua"))
			e.Refresh()
		Next
		
		'reference to last entity. for scaling test. see postupdate()
'		_ent = e
	End Method
	

	Method PreRender(tween:Double)
		'do this before render so we use camera offset
		_quadTreeSystem.Render()
		_collisionSystem.Render(150)
	End Method
	

	Method PostRender(tween:Double)
	?Debug
		TRenderState.Push()
		TRenderState.Reset()
		
		SetAlpha(0.5)
		SetColor(0, 255, 0)
		
		DrawText("Entities: " + _world.GetEntityManager().GetEntityCount(), 0, 0)
		
		_game._renderSystem.DebugText(20)
		
		'camera info
'		DrawText(_camerasystem.PosToString(), 0, 60)
'		DrawText(_camerasystem.ViewToString(), 0, 75)
		
		'quadtree world size
'		Local r:TRect = _quadtreesystem._area
'		DrawText("world rect:" + Int(r.GetX()) + "," + Int(r.GetY()) + " to " + ..
'				(Int(r.GetX() + r.GetWidth())) + "," + (Int(r.GetY() + r.GetHeight())), 0, 120)
				
		TRenderState.Pop()
	?
	End Method
	

	Method Update(delta:Double)
'
		'update collision system so we can print them
		_collisionSystem.CollidingWith(_player, _game)
		
'		Local blocks:TBag = _world.GetGroupManager().GetEntities("blocks")
'		If blocks
'			For Local i:Int = 0 To blocks.GetSize() - 1
'			Next
'		EndIf		
	End Method
	
	
	'my additional update code.
	Method PostUpdate(delta:Double)
		'end game?
		If KeyHit(KEY_ESCAPE) Then _game.RequestStop()

		'scale entity so we can see that collision is working on scaled entities.
'		Local c:TComponentMapper = New TComponentMapper.Create(New TTransformComponent, _world)
'		Local component:TTransformComponent = TTransformComponent(c.Get(_ent))
'		If KeyDown(KEY_Q) Then component.SetScale(component.GetScaleX() + 0.5, component.GetScaleY())
'		If KeyDown(KEY_A) Then component.SetScale(component.GetScaleX() - 0.5, component.GetScaleY())
	End Method

End Type



